<!DOCTYPE html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>highlight.R</title>
<meta name="generator" content="KF5::SyntaxHighlighting - Definition (R Script) - Theme (Breeze Light)"/>
</head><body style="background-color:#ffffff;color:#1f1c1b"><pre>
<span style="color:#607880;font-weight:bold;">## R Script Sample File</span>
<span style="color:#607880;font-weight:bold;">## Source: http://www.rexamples.com</span>

<span style="color:#607880;font-weight:bold;">## Basics</span>
a <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#b08000;">42</span>
A <span style="color:#006e28;font-weight:bold;">&lt;-</span> a <span style="color:#3daee9;">*</span> <span style="color:#b08000;">2</span>  <span style="color:#898887;"># R is case sensitive</span>
<span style="color:#644a9b;">print</span>(a)
<span style="color:#644a9b;">cat</span>(A, <span style="color:#bf0303;">&quot;</span><span style="color:#3daee9;">\n</span><span style="color:#bf0303;">&quot;</span>) <span style="color:#898887;"># &quot;84&quot; is concatenated with &quot;\n&quot;</span>
<span style="font-weight:bold;">if</span>(A<span style="color:#3daee9;">&gt;</span>a) <span style="color:#898887;"># true, 84 &gt; 42</span>
{
  <span style="color:#644a9b;">cat</span>(A, <span style="color:#bf0303;">&quot;&gt;&quot;</span>, a, <span style="color:#bf0303;">&quot;</span><span style="color:#3daee9;">\n</span><span style="color:#bf0303;">&quot;</span>)
}

<span style="color:#607880;font-weight:bold;">## Functions</span>
Square <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="font-weight:bold;">function</span>(x) {
  <span style="color:#644a9b;">return</span>(x<span style="color:#3daee9;">^</span><span style="color:#b08000;">2</span>)
}

<span style="color:#644a9b;">print</span>(<span style="color:#644a9b;">Square</span>(<span style="color:#b08000;">4</span>))
<span style="color:#644a9b;">print</span>(<span style="color:#644a9b;">Square</span>(<span style="color:#0057ae;">x=</span><span style="color:#b08000;">4</span>)) <span style="color:#898887;"># same thing</span>

<span style="color:#644a9b;">DoSomething</span>(<span style="color:#0057ae;">color=</span><span style="color:#bf0303;">&quot;red&quot;</span>,<span style="color:#0057ae;">number=</span><span style="color:#b08000;">55</span>)

<span style="color:#607880;font-weight:bold;">## Countdown</span>
countdown <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="font-weight:bold;">function</span>(from)
{
  <span style="color:#644a9b;">print</span>(from)
  <span style="font-weight:bold;">while</span>(from<span style="color:#3daee9;">!=</span><span style="color:#b08000;">0</span>)
  {
    <span style="color:#644a9b;">Sys.sleep</span>(<span style="color:#b08000;">1</span>)
    from <span style="color:#006e28;font-weight:bold;">&lt;-</span> from <span style="color:#3daee9;">-</span> <span style="color:#b08000;">1</span>
    <span style="color:#644a9b;">print</span>(from)
  }
}

<span style="color:#644a9b;">countdown</span>(<span style="color:#b08000;">5</span>)

<span style="color:#607880;font-weight:bold;">## Reading user input</span>
readinteger <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="font-weight:bold;">function</span>()
{
  n <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">readline</span>(<span style="color:#0057ae;">prompt=</span><span style="color:#bf0303;">&quot;Enter an integer: &quot;</span>)
  n <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">as.integer</span>(n)
  <span style="font-weight:bold;">if</span> (<span style="color:#644a9b;">is.na</span>(n)){
    n <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">readinteger</span>()
  }
  <span style="color:#644a9b;">return</span>(n)
}
<span style="color:#644a9b;">print</span>(<span style="color:#644a9b;">readinteger</span>())

readinteger <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="font-weight:bold;">function</span>()
{
  n <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">readline</span>(<span style="color:#0057ae;">prompt=</span><span style="color:#bf0303;">&quot;Enter an integer: &quot;</span>)
  <span style="font-weight:bold;">if</span>(<span style="color:#3daee9;">!</span><span style="color:#644a9b;">grepl</span>(<span style="color:#bf0303;">&quot;^[0-9]+$&quot;</span>,n))
  {
    <span style="color:#644a9b;">return</span>(<span style="color:#644a9b;">readinteger</span>())
  }

  <span style="color:#644a9b;">return</span>(<span style="color:#644a9b;">as.integer</span>(n))
}
<span style="color:#644a9b;">print</span>(<span style="color:#644a9b;">readinteger</span>())

<span style="color:#607880;font-weight:bold;">## Guess a random number game</span>
readinteger <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="font-weight:bold;">function</span>()
{
  n <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">readline</span>(<span style="color:#0057ae;">prompt=</span><span style="color:#bf0303;">&quot;Enter an integer: &quot;</span>)
  <span style="font-weight:bold;">if</span>(<span style="color:#3daee9;">!</span><span style="color:#644a9b;">grepl</span>(<span style="color:#bf0303;">&quot;^[0-9]+$&quot;</span>,n))
  {
    <span style="color:#644a9b;">return</span>(<span style="color:#644a9b;">readinteger</span>())
  }
  <span style="color:#644a9b;">return</span>(<span style="color:#644a9b;">as.integer</span>(n))
}

<span style="color:#898887;"># real program start here</span>

num <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">round</span>(<span style="color:#644a9b;">runif</span>(<span style="color:#b08000;">1</span>) <span style="color:#3daee9;">*</span> <span style="color:#b08000;">100</span>, <span style="color:#0057ae;">digits =</span> <span style="color:#b08000;">0</span>)
guess <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#3daee9;">-</span><span style="color:#b08000;">1</span>

<span style="color:#644a9b;">cat</span>(<span style="color:#bf0303;">&quot;Guess a number between 0 and 100.</span><span style="color:#3daee9;">\n</span><span style="color:#bf0303;">&quot;</span>)

<span style="font-weight:bold;">while</span>(guess <span style="color:#3daee9;">!=</span> num)
{
  guess <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">readinteger</span>()
  <span style="font-weight:bold;">if</span> (guess <span style="color:#3daee9;">==</span> num)
  {
    <span style="color:#644a9b;">cat</span>(<span style="color:#bf0303;">&quot;Congratulations,&quot;</span>, num, <span style="color:#bf0303;">&quot;is right.</span><span style="color:#3daee9;">\n</span><span style="color:#bf0303;">&quot;</span>)
  }
  <span style="font-weight:bold;">else</span> <span style="font-weight:bold;">if</span> (guess <span style="color:#3daee9;">&lt;</span> num)
  {
    <span style="color:#644a9b;">cat</span>(<span style="color:#bf0303;">&quot;It's bigger!</span><span style="color:#3daee9;">\n</span><span style="color:#bf0303;">&quot;</span>)
  }
  <span style="font-weight:bold;">else</span> <span style="font-weight:bold;">if</span>(guess <span style="color:#3daee9;">&gt;</span> num)
  {
    <span style="color:#644a9b;">cat</span>(<span style="color:#bf0303;">&quot;It's smaller!</span><span style="color:#3daee9;">\n</span><span style="color:#bf0303;">&quot;</span>)
  }
}

<span style="color:#607880;font-weight:bold;">## Lists</span>
<span style="color:#644a9b;">sum</span>(<span style="color:#b08000;">0</span><span style="color:#3daee9;">:</span><span style="color:#b08000;">9</span>)
<span style="color:#644a9b;">append</span>(LETTERS[<span style="color:#b08000;">1</span><span style="color:#3daee9;">:</span><span style="color:#b08000;">13</span>],letters[<span style="color:#b08000;">14</span><span style="color:#3daee9;">:</span><span style="color:#b08000;">26</span>])
<span style="color:#644a9b;">c</span>(<span style="color:#b08000;">1</span>,<span style="color:#b08000;">6</span>,<span style="color:#b08000;">4</span>,<span style="color:#b08000;">9</span>)<span style="color:#3daee9;">*</span><span style="color:#b08000;">2</span>
something <span style="color:#006e28;font-weight:bold;">&lt;-</span> <span style="color:#644a9b;">c</span>(<span style="color:#b08000;">1</span>,<span style="color:#b08000;">4</span>,letters[<span style="color:#b08000;">2</span>])  <span style="color:#898887;"># indices start at one, you get (1,4,&quot;b&quot;)</span>
<span style="color:#644a9b;">length</span>(something)

<span style="color:#607880;font-weight:bold;">## Pipe</span>
diamonds <span style="color:#3daee9;">%&gt;%</span>
  <span style="color:#644a9b;">filter</span>(price <span style="color:#3daee9;">&gt;</span> <span style="color:#b08000;">1000</span>) <span style="color:#3daee9;">%&gt;%</span>
  <span style="color:#644a9b;">group_by</span>(cut) <span style="color:#3daee9;">%&gt;%</span>
  <span style="color:#644a9b;">tally</span>() <span style="color:#3daee9;">%&gt;%</span> 
  <span style="color:#644a9b;">rename</span>(<span style="color:#0057ae;">tally =</span> n) <span style="color:#3daee9;">%&gt;%</span> 
  <span style="color:#644a9b;">arrange</span>(<span style="color:#3daee9;">-</span>tally) <span style="color:#3daee9;">%&gt;%</span> 
  <span style="color:#644a9b;">mutate</span>(<span style="color:#0057ae;">pct =</span> tally <span style="color:#3daee9;">/</span> <span style="color:#644a9b;">sum</span>(tally)) <span style="color:#006e28;font-weight:bold;">-&gt;</span> df

<span style="color:#607880;font-weight:bold;">## ggplot2 </span>
plot <span style="color:#006e28;font-weight:bold;">=</span> <span style="color:#644a9b;">ggplot</span>(diamonds, <span style="color:#644a9b;">aes</span>(<span style="color:#0057ae;">x =</span> price, <span style="color:#0057ae;">y =</span> carat)) <span style="color:#3daee9;">+</span>
  <span style="color:#644a9b;">geom_point</span>(<span style="color:#0057ae;">alpha =</span> <span style="color:#b08000;">0.3</span>, <span style="color:#0057ae;">colour =</span> <span style="color:#bf0303;">'steelblue'</span>) <span style="color:#3daee9;">+</span>
  <span style="color:#644a9b;">labs</span>(
    <span style="color:#0057ae;">title =</span> <span style="color:#bf0303;">&quot;ggplot diamonds&quot;</span>,
    <span style="color:#0057ae;">x =</span> <span style="color:#bf0303;">&quot;Price, $&quot;</span>,
    <span style="color:#0057ae;">y =</span> <span style="color:#bf0303;">&quot;Carat&quot;</span>
  ) <span style="color:#3daee9;">+</span>
  <span style="color:#644a9b;">facet_wrap</span>(<span style="color:#3daee9;">~</span> cut) <span style="color:#3daee9;">+</span>
  <span style="color:#644a9b;">theme_minimal</span>()

plot <span style="color:#3daee9;">+</span> <span style="color:#644a9b;">coord_flip</span>()
</pre></body></html>
